package top.juntech.msweatherdata.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import top.juntech.msweatherdata.service.WeatherDataService;
import top.juntech.msweatherdata.utils.StringUtils;
import top.juntech.msweatherdata.vo.WeatherResponse;

import java.util.concurrent.TimeUnit;

@Service
public class WeatherDataServiceImpl implements WeatherDataService {
    private static final Logger logger = LoggerFactory.getLogger(WeatherDataServiceImpl.class);
    private static final long TIME_OUT = 1800L;
    private static final String WEATHER_NAME_URI = "http://wthrcdn.etouch.cn/weather_mini?city=";
    private static final String WEATHER_ID_URI = "http://wthrcdn.etouch.cn/weather_mini?citykey=";

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public WeatherResponse getWeatherDataByCityId(String cityId) {
        String uri = WEATHER_ID_URI + cityId;
        return this.dogetWeatherResponse(uri);
    }

    @Override
    public WeatherResponse getWeatherDataByCityName(String cityName) {
        String uri = WEATHER_NAME_URI+cityName;
        return this.dogetWeatherResponse(uri);
    }

    public WeatherResponse dogetWeatherResponse(String uri){
        WeatherResponse weatherResponse = null;
        String key = uri;
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ObjectMapper objectMapper = new ObjectMapper();
        String strBody = null;
//        先查询是否有缓存
        if(stringRedisTemplate.hasKey(key)){
            logger.info("redis has data!");
            strBody = ops.get(key);
        }else{
            logger.info("redis don't have data!");
            throw new RuntimeException("缓存中没有该数据!");
//            ResponseEntity<String> respBody = restTemplate.getForEntity(key, String.class);
////            ResponseEntity<WeatherResponse> respBody = restTemplate.getForEntity(key, WeatherResponse.class);
//            if(respBody.getStatusCodeValue() == 200){
//                try {
//                    strBody = StringUtils.conventFromGzip(respBody.getBody());
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//
//            }
////            将数据存入缓存
//            ops.set(key,strBody,TIME_OUT, TimeUnit.SECONDS);
        }

        try{
            weatherResponse = objectMapper.readValue(strBody, WeatherResponse.class);
            System.out.println(weatherResponse);
        }catch (Exception e){
//            throw new RuntimeException(e);
            logger.error("error!",e);
        }
        return weatherResponse;
    }
}
